﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using ClassLibrary;
using Param = ClassLibrary.Parameters.Values;
using System.Resources;

namespace ControlLibrary
{
    public partial class CategoryList : UserControl
    {
        public delegate void OnCheckChanged();
        [Category("Configuration"), Browsable(true), Description("")]
        public event OnCheckChanged onCheckChanged;

        public delegate void OnConfigClick();
        [Category("Configuration"), Browsable(true), Description("")]
        public event OnConfigClick onConfigClick;

        bool _init = false;

        public void UpdateUI()
        {
            ResourceManager _RM = new ResourceManager("ControlLibrary.CategoryList", typeof(CategoryList).Assembly);
            try
            {
                linkLabel_AddCat.Text = _RM.GetString("linkLabel_AddCat.Text");   
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("CategoryList", "UpdateUI", e, Param.RM.GetString("MB_Error_UpdateUI"));
            }
        }

        public List<string> CheckedItems
        {
            get
            {
                List<string> res = new List<string>();
                try
                {
                    foreach (Control c in flowLayoutPanel1.Controls)
                    {
                        CategoryItem item = c as CategoryItem;
                        if (null != item)
                        {
                            if (item.Check)
                                res.Add(item.Title);
                        }
                    }
                    return res;
                }
                catch (Exception e)
                {
                    ErrorManager.WriteLog("CategoryList", "CheckedItems", e, Param.RM.GetString("CL_Error_ChkItem_Get"));
                }
                return res;
            }
            set
            {
                try
                {
                    _init = true;
                    foreach (Control c in flowLayoutPanel1.Controls)
                    {
                        CategoryItem item = c as CategoryItem;
                        if (null != item)
                        {
                            if (value.Contains(item.Title))
                                item.Check = true;
                            else item.Check = false;
                        }
                    }
                    _init = false;
                }
                catch (Exception e)
                {
                    _init = false;
                    ErrorManager.WriteLog("CategoryList", "CheckedItems", e, Param.RM.GetString("CL_Error_ChkItem_Set"));
                }
            }
        }

        public List<string> Items
        {
            set 
            {
                try
                {
                    clear();
                    bool RC = true;
                    foreach (string s in value)
                    {
                        if (true == RC)
                            RC = addItem(s);
                        else
                            throw new Exception("AddItem failed");
                    }
                }
                catch (Exception e)
                {
                    ErrorManager.WriteLog("CategoryList", "Items", e, Param.RM.GetString("CL_Error_SetItem"));
                }
            }
        }

        public CategoryList()
        {
            InitializeComponent();
        }

        public void clear()
        {
            flowLayoutPanel1.Controls.Clear();
            flowLayoutPanel1.Controls.Add(linkLabel_AddCat);

        }

        private bool addItem(string title)
        {
            try
            {
                CategoryItem item = new CategoryItem(title);

                item.Width =
                    flowLayoutPanel1.Width -
                    flowLayoutPanel1.Margin.Right -
                    flowLayoutPanel1.Margin.Left -
                    SystemInformation.VerticalScrollBarWidth;

                flowLayoutPanel1.Controls.Add(item);

                item.onCheckChanged += new CategoryItem.OnCheckChanged(item_onCheckChanged);

                setHighlightMaxWidth();

                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("CategoryList", "addItem", e);
                return false;
            }
        }

        private bool getMaxWidth(out int maxWidth)
        {
            maxWidth = 0;
            try
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    CategoryItem item = c as CategoryItem;
                    if (null != item)
                        maxWidth = Math.Max(maxWidth, item.ContentWidth);
                }
                return true;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("CategoryList", "getMaxWidth", e);
                return false;
            }
        }

        private bool setHighlightMaxWidth()
        {
            try
            {
                int maxWidth;
                bool RC = getMaxWidth(out maxWidth);

                if (true == RC)
                {
                    foreach (Control c in flowLayoutPanel1.Controls)
                    {
                        CategoryItem item = c as CategoryItem;
                        if (null != item)
                        {
                            item.HighlightWidth = maxWidth;
                        }
                    }
                }
                else throw new Exception("getMaxWidth failed");
                return RC;
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("CategoryList", "setHighlightMaxWidth", e);
                return false;
            }
        }

        private void flowLayoutPanel1_SizeChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (Control c in flowLayoutPanel1.Controls)
                {
                    c.Width =
                    flowLayoutPanel1.Width -
                    flowLayoutPanel1.Margin.Right -
                    flowLayoutPanel1.Margin.Left -
                    SystemInformation.VerticalScrollBarWidth;
                }

                flowLayoutPanel1.PerformLayout();
            }
            catch (Exception ex)
            {
                ErrorManager.WriteLog("CategoryList", "flowLayoutPanel1_SizeChanged", ex);
            }
        }

        #region SEND EVENTS

        private void linkLabel_AddCat_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (null != onConfigClick)
                onConfigClick();
        }

        private void checkedListBox_categories_SelectedValueChanged(object sender, EventArgs e)
        {
            if (null != onCheckChanged)
                onCheckChanged();
        }

        private void item_onCheckChanged()
        {
            if (!_init)
            {
                if (null != onCheckChanged)
                    onCheckChanged();
            }
        }

        #endregion
    }
}
